<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>APP下载</title>
	<link rel="icon" href="../../img/icon.png" type="image/x-icon"/>
	<link rel="stylesheet" type="text/css" href="../../css/mui.css"/>
	<link rel="stylesheet" type="text/css" href="../../css/mui.min.css"/>
	<link rel="stylesheet" type="text/css" href="../../css/app.css"/>
</head>
<body>
<!-- 顶部菜单 -->
<ul id="top">
	<!-- <li class="addBug back" onclick="mui.back();">返回</li> -->
	<li class="addBug back" onclick="jumpToMy()">返回</li>
	<li class="addBug">APP版本</li>
	<li class="addBug pub">&nbsp;</li>
</ul>
<!-- 主体 -->
<div style="height: 50px;"></div>
<ul class="mui-table-view" id="list">
<!--    <li class="mui-table-view-cell">
        <a class="mui-navigate-right">版本V</a>
    </li> -->
</ul>
<div style="height: 50px;"></div>
<!-- 底部菜单 -->
<ul id="foot">
	<li class="noclick" onclick="jumpToIndex()">BUG</li>
	<li class="noclick" onclick="jumpToHistory()">历史</li>
	<li class="noclick" onclick="jumpToMore()">更多</li>
	<li onclick="jumpToMy()">我的</li>
</ul>
</body>
</html>


<script type="text/javascript" src="../../js/mui.min.js"></script>
<script type="text/javascript" src="../../js/app.js"></script>
<script type="text/javascript">
	var domain = getDomain();
	var appVersion = cacheGet(appVersionKey);
	(function($, owner) {
		getIndex();
	}(mui, window.app = {}));
	//获取版本
	function getIndex(){
		mui.ajax(domain+'/app/version/index',{
			data:{
				//
			},
			dataType:'json',//服务器返回json格式数据
			type:'post',//HTTP请求类型
			timeout:10000,//超时时间设置为10秒；
			// headers:{'Content-Type':'application/json'},
			headers:{'Content-Type':'application/x-www-form-urlencoded'}, 
			async: true,  //async: true || false, // 异步 || 同步
			crossDomain: true,  //强制跨域
			success:function(res){
				console.log(res);
				var list = '';
				if (res.code!=0) {
					list += '<li class="mui-table-view-cell">';
					list += '<a class="mui-navigate-right">没有数据</a>';
					list += '</li>';
					mui("#list")[0].innerHTML = list;
					return
				}
				var data = res.data;
				for (var i=0;i<data.length;i++) {
					list += '<li class="mui-table-view-cell">';
					list += '<a class="mui-navigate-right">';
					list += '版本V'+data[i].version;
					if (data[i].version > appVersion) {
						list += ' &nbsp; <span style="color:red;">最新版</span>';
					}
					list += '</a>';
					list += '</li>';
				}
				mui("#list")[0].innerHTML = list;
				//如果有最新版本，加一个弹框
				if (data[0].version > appVersion) {
					var version = data[0].version;
					var link = data[0].link;
					mui.confirm('需要升级版本么？','新版升级提示',['取消','确认'],function (e) {
						// console.log(e.index,link);
						if (e.index==1) {
							setDownload(version,link);
						}
					},'div');
				}
			},
			error:function(xhr,type,errorThrown){
				// console.log('网络错误');
				mui.toast(xhr+type+'网络错误！',{
					duration:'long', type:'div',
				});
			}
		});
	}
	//APP下载安装
	function setDownload(version,link){
		// console.log(version,link); return
		mui.plusReady(function () {
			// plus.runtime.openWeb(link);
			// plus.runtime.openURL(link);  //下载
			plus.nativeUI.toast("正在准备下载，请稍后！");
			var dtask = plus.downloader.createDownload(link, {}, function (d, status) {
				if (status == 200) {
					mui.toast('下载成功，准备安装！',{ 
						duration:'long', type:'div',
					});
					var path = d.filename;
					plus.runtime.install(path); // 自动安装apk文件
					mui.toast('安装成功！',{ 
						duration:'long', type:'div',
					});
				} else {
					plus.nativeUI.alert('版本更新失败:' + status);
				}
			});
			dtask.start();
			//下载安装进度显示
			dtask.addEventListener("statechanged", function(task, status) {
				switch(task.state) {
					case 1: // 开始
						console.log('开始');
						mui.toast('开始！',{ 
							duration:'long', type:'div',
						});
						break;
					case 2: //已连接到服务器
						console.log('已连接到服务器');
						mui.toast('已连接到服务器！',{ 
							duration:'long', type:'div',
						});
						break;
					case 3: // 已接收到数据
						console.log('已接收到数据');
						mui.toast('开始！',{ 
							duration:'long', type:'div',
						});
						var current = parseInt(100 * task.downloadedSize / task.totalSize);
						plus.notification.setProgress(current); //插件调用
						break;
					case 4: // 下载完成 
						console.log('下载完成 ');
						mui.toast('下载完成',{ 
							duration:'long', type:'div',
						});
						plus.notification.compProgressNotification("下载完成"); //插件调用
						plus.runtime.install(task.filename, {}, function() {
							//成功安装回调，执行重启app操作
							plus.runtime.restart();
						}, function(DOMException) {
							console.log(JSON.stringify(DOMException));
							mui.toast("安装失败");
						});
						break;
				}
			});
			//更新本地APP版本缓存
			var appVersion = {
				'version':version,
				'link':link
			};
			cacheSet(appVersionKey,appVersion);
		});
	}
</script>